<?php

namespace App\Http\Controllers\Wechat\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\UserAddressRepo;
use App\Repos\ShopRepo;
use App\Http\Requests\Api\UserAddressRequest;

class AddressController extends Controller
{
    public function index(UserAddressRequest $request)
    {
    	$rs['status'] = false;
    	$where['user_id'] = $request->user_id; 
    	$num = (int)$request->num?$request->num:5;  
        if($request->shop_id)
        {
            $where['shop_id'] = (int)$request->shop_id;
        } 	
    	$list = UserAddressRepo::getPages($where,$num);
    	if($list) {
    		$rs['status'] = true;
    		$rs['data'] = $list;
    		return response()->json($rs);
    	}

    	return response()->json($rs);
    	
    }

    public function delete(UserAddressRequest $request)
    {
        $rs['status'] = false;
        $where['user_id'] = $request->user_id;
        $where['id']      = $request->address_id;
        $flag = UserAddressRepo::destroy($where);
        if($flag) {
            $rs['status'] = true;     
            return response()->json($rs);       
        }
        return response()->json($rs);
    }

    public function create(UserAddressRequest $request)
    {
        $rs['status'] = false;

        $validatedData = $request->validate([
            'shop_id' => 'required',        
        ]);

        $data = $request->pure();
        // 判断用户选择的地址是否超出了店铺3公里
      
        $dist = $this->shopDist($data['shop_id'],$data['lnglat']);
        if($dist===false) {
            $rs['msg'] = '超出商户配送距离';
            return response()->json($rs);
        }
        $flag = UserAddressRepo::create($data);
        if($flag) {
            $rs['status'] = true;
            return response()->json($rs);
        }
        return response()->json($rs);
    }   

    public function show(UserAddressRequest $request)
    {
        $rs['status'] = false;
        $where['user_id'] = $request->user_id;
        $where['id']      = $request->address_id;
        $data = UserAddressRepo::find($where);
        if($data) {
            $rs['status'] = true;
            $rs['data']   = $data;
            return response()->json($rs);
        }
        return response()->json($rs);
    }

    public function update(UserAddressRequest $request)
    {
        $rs['status'] = false;
        $validatedData = $request->validate([
            'shop_id' => 'required',        
        ]);
        $where['user_id'] = $request->user_id;
        $where['id']      = $request->address_id;
        $data = $request->pure();
        $dist = $this->shopDist($data['shop_id'],$data['lnglat']);
        if($dist===false) {
            $rs['msg'] = '超出商户配送距离';
            return response()->json($rs);
        }
        $flag = UserAddressRepo::update($where,$data);
        if($flag) {
            $rs['status'] = true;           
            return response()->json($rs);
        }
        return response()->json($rs);
    }

    private function shopDist($shop_id,$lnglat)
    { 
        $shop = ShopRepo::find(['id'=>$shop_id]);

        $shopCoord = explode(',', $shop->lnglat);        
        $lnglat    = explode(',', $lnglat);
        $dist = getDistance($lnglat[0],$lnglat[1],$shopCoord[0],$shopCoord[1]);
       
        if($dist>3) {
            return false;
        }
        return true;
        
    }

    
}
